﻿using Devil;
using System;
using System.Collections.Generic;
using System.Text;

namespace TableTranslate
{
    internal enum TaskType
    {
        None,
        UpdateTextID,
        ImportTranslateText,
        UploadTextXml,
        Quit,
    }

    internal class MainLooper : ILoopCommand
    {
        public TaskType action;
        string TITLE;
 //           = @"=================== 功能选择 ======================
 //[1] 更新文本表 ID
 //[2] 导入翻译文本
 //[3] 导入 XML 文本
 //[4] 退出";
        string TILE = @"---------------------------------------------------";

        Dictionary<TaskType, ICommand> mCmds;

        class Quit: ICommand
        {
            MainLooper mLooper;
            public Quit(MainLooper looper)
            {
                this.mLooper = looper;
            }
            public void Execute()
            {
                mLooper.action = TaskType.Quit;
            }

            public override string ToString()
            {
                return "退出";
            }
        }

        public MainLooper()
        {
            var buf = new StringBuilder();
            buf.Append("=================== 功能选择 ======================");

            mCmds = new Dictionary<TaskType, ICommand>();
            ICommand cmd = new UpdateTextsID();
            mCmds[TaskType.UpdateTextID] = cmd;
            buf.Append("\n[").Append((int)TaskType.UpdateTextID).Append("] ").Append(cmd.ToString());

            cmd = new ImportTranslateTexts();
            mCmds[TaskType.ImportTranslateText] = cmd;
            buf.Append("\n[").Append((int)TaskType.ImportTranslateText).Append("] ").Append(cmd.ToString());

            cmd = new UpdateXmlTexts();
            mCmds[TaskType.UploadTextXml] = cmd;
            buf.Append("\n[").Append((int)TaskType.UploadTextXml).Append("] ").Append(cmd.ToString());

            cmd = new Quit(this);
            mCmds[TaskType.Quit] = cmd;
            buf.Append("\n[").Append((int)TaskType.Quit).Append("] ").Append(cmd.ToString());

            TITLE = buf.ToString();
        }

        public void Execute(float deltaTime)
        {
            Console.WriteLine(TITLE);
            var input = Program.ReadText(null);
            Console.WriteLine(TILE);
            int n;
            ICommand cmd;
            if (int.TryParse(input, out n) && mCmds.TryGetValue((TaskType)n, out cmd))
            {
                Console.WriteLine(cmd.ToString());
                cmd.Execute();
                Console.WriteLine();
            }
        }
    }
}
